package com.ktg.mes.chanjet.task;

import com.ktg.common.exception.BussinessException;
import com.ktg.common.utils.StringUtils;
import com.ktg.mes.chanjet.entity.TpItemType;
import com.ktg.mes.chanjet.response.InventoryClassResponse;
import com.ktg.mes.chanjet.service.TpItemTypeService;
import com.ktg.mes.chanjet.spi.ChanjetSpi;
import com.ktg.mes.md.domain.ItemType;
import com.ktg.mes.md.service.IItemTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**
 * T+ 仓库存货同步
 */

@Slf4j
@Component("inventoryClassSyncDataTask")
public class InventoryClassSyncDataTask {

    @Autowired
    IItemTypeService itemTypeService;
    @Autowired
    ChanjetSpi chanjetSpi;


    public void execute(){
        try{
            InventoryClassResponse inventoryClass = chanjetSpi.getInventoryClass();
            List<Map> data = inventoryClass.getData();
            List<ItemType> list = new ArrayList<>();
            Map<String,String> nodeMap = new HashMap<>();
            for (Map map:data) {
                ItemType itemType = new ItemType();
                itemType.setItemTypeCode((String)map.get("code"));
                itemType.setItemTypeName((String)map.get("name"));
                if(StringUtils.isNotNull(map.get("parent.code"))) itemType.setParentTypeCode((String)map.get("parent.code"));
                itemType.setItemTypeId(new BigDecimal((Double)map.get("id")).longValue());
                if(StringUtils.isNotNull(map.get("parent.id"))){
                    long parentId = new BigDecimal((Double) map.get("parent.id")).longValue();
                    itemType.setParentTypeId(parentId);
                    if(parentId > 0){
                        nodeMap.put(itemType.getItemTypeId() +"",parentId+"");
                    }
                }else{
                    itemType.setParentTypeId(0L);
                }
                itemType.setEnableFlag("Y");
                itemType.setUpdateTime(new Date());
                list.add(itemType);
            }
            for (ItemType type : list) {
                Long id = type.getItemTypeId();
                if(nodeMap.get(id+"") != null && type.getParentTypeId() > 0){
                    StringBuffer sb = new StringBuffer();
                    sb.append(nodeMap.get(id+""));
                    getParentId(nodeMap, type.getParentTypeId(), sb);
                    type.setAncestors(sb.toString());
                }else{
                   type.setAncestors("0");
                }
            }
            itemTypeService.saveOrUpdateBatch(list);
        }catch (Exception ex){
            log.error("同步T+存货分类 信息出错",ex);
            throw new BussinessException("执行失败");
        }
    }
    private void getParentId(Map map,long id,StringBuffer ids){
        if(map.get(id+"") != null){
           String parentId =  (String) map.get(id + "");
          ids.append(","+parentId);
           getParentId(map,new BigDecimal(parentId).longValue(),ids);
        }
    }
}
